package com.android.common.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;

public class DialogUtil {
	private static final String TAG = "DialogUtil";

	public static ProgressDialog createProgressDialog(Context context, int id) {
		return createProgressDialog(context, context.getString(id));
	}

	public static ProgressDialog createProgressDialog(Context context,
			String msg) {
		ProgressDialog pd = new ProgressDialog(context);
		pd.setMessage(msg);
		pd.setCanceledOnTouchOutside(false);
		return pd;
	}

	public static Dialog createDialog(Context context,
			OnClickListener positiveListener,
			OnClickListener onNegativeListener,
			OnDismissListener onDismissListener, String message, String title) {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
		if (StringUtil.isNotBlank(message)) {
			dialogBuilder.setMessage(message);
		}
		if (StringUtil.isNotBlank(title)) {
			dialogBuilder.setTitle(title);
		}
		if (positiveListener == null) {
			positiveListener = onNegativeListener;
		}
		dialogBuilder.setPositiveButton("确定", positiveListener);
		if (onNegativeListener != null) {
			dialogBuilder.setNegativeButton("取消", onNegativeListener);
		}
		Dialog dialog = dialogBuilder.create();
		if (onDismissListener != null) {
			dialog.setOnDismissListener(onDismissListener);
		}
		return dialog;
	}

	public static OnClickListener onNegativeListener = new OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
		}
	};

	public static class SuccessPositiveListener implements
			DialogInterface.OnClickListener {
		Activity mActivity;

		public SuccessPositiveListener(Activity activity) {
			mActivity = activity;
		}

		@Override
		public void onClick(DialogInterface dialog, int which) {
			mActivity.finish();
		}

	};

	public static void dimissDialog(Dialog dialog) {
		if (dialog != null && dialog.isShowing())
			dialog.dismiss();
	}
}
